package com.fancy.mobileschool.ui;


import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.fancy.mobileschool.R;
import com.fancy.mobileschool.ui.adapt.SDFileListAdapter;
import com.fancy.mobileschool.ui.widget.DividerItemDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class UploadFileActivity extends AppCompatActivity{

    private RecyclerView recyclerView;

    private List<File> files;

    private File currentDir;

    private MyOnItemClickListener listener;

    private File rootDir = Environment.getExternalStorageDirectory();

    private SDFileListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_file);
        listener = new MyOnItemClickListener();
        recyclerView = (RecyclerView) findViewById(R.id.file_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        files = new ArrayList<>();
        File file = Environment.getExternalStorageDirectory();
        currentDir = file;
        File[] f = file.listFiles();
        for (int i = 0 ; i < f.length ; i++) {
            files.add(f[i]);
        }

        adapter = new SDFileListAdapter(files, getApplicationContext(), listener);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!currentDir.getName().equals(rootDir.getName())) {
                File superDir = currentDir.getParentFile();
                File[] tempfiles = superDir.listFiles();
                List<File> list = new ArrayList<>(tempfiles.length);
                for (int i = 0 ; i < tempfiles.length ; i++)
                    list.add(tempfiles[i]);

                files = list;
                currentDir = superDir;

                adapter.refreshData(list);
            } else{
                Intent i = new Intent();
                i.putExtra("file", "");
                setResult(RESULT_OK, i);
                finish();
            }


        }

        return true;
    }

    class MyOnItemClickListener implements SDFileListAdapter.OnItemClickListener {

        @Override
        public void onItemClick(View view, int position) {
            File file = files.get(position);
            if (file.isDirectory()) {
                List<File> tempFiles = new ArrayList<>();
                File[] listFiles = file.listFiles();
                for (int i=0 ; i < listFiles.length ; i++)
                    tempFiles.add(listFiles[i]);

                files = tempFiles;
                currentDir = file;
                adapter.refreshData(files);
            }else {
                Intent i = new Intent();
                i.putExtra("file", files.get(position).getAbsolutePath());
                setResult(RESULT_OK, i);
                finish();
            }
        }
    }


}
